﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;

namespace SO_DataFetcher.PubFunctions.Paraments
{
    public class PubParament
    {
        public static Dictionary<string, ParamentStruct> Paraments = new Dictionary<string, ParamentStruct>();    //公共字典集
        public static DataSet DatasetParametns = null;    //公共数据集     
        public static DataTable DataTableParaments = new DataTable();
        private static readonly string XmlPath = PubCommon.XmlParamentsPath;
        private static bool _isXmlExist = false;

        /// <summary>
        /// 初始化参数
        /// </summary>
        public static void Init()
        {
            try
            {
                IsXmlExist();

                if (_isXmlExist)
                    GetTableParaments();
                else
                {
                    DataTableParaments = new DataTable();
                    DataTableParaments.Columns.Add("Index", typeof(string));
                    DataTableParaments.Columns.Add("Name", typeof(string));
                    DataTableParaments.Columns.Add("CtrStyle", typeof(string));
                    DataTableParaments.Columns.Add("CtrLabel", typeof(string));
                    DataTableParaments.Columns.Add("CtrTip", typeof(string));
                    DataTableParaments.Columns.Add("CtrValueRange", typeof(string));
                    DataTableParaments.Columns.Add("CtrValue", typeof(string));
                    DataTableParaments.Columns.Add("CtrExtName", typeof(string));
                }

                PubLog.WriteLog("初始化参数配置文件完成");
            }
            catch (Exception ex)
            {
                PubLog.WriteLog("初始化参数配置文件错误：" + ex.Message);
            }
        }

        public static void IsXmlExist()
        {
            _isXmlExist = File.Exists(XmlPath);
        }

        /// <summary>                                                        
        /// 更新连接数据
        /// </summary>                                                     
        private static void GetTableParaments()
        {
            DatasetParametns = PubXml.GetDataSetFromXml(XmlPath);

            if (DatasetParametns.Tables.Count > 0)
            {
                DataTableParaments = DatasetParametns.Tables[0];

                if (DataTableParaments != null && DataTableParaments.Rows.Count > 0)
                    GetDictionaryParaments();
            }
        }


        /// <summary>
        /// 存储为字典变量
        /// </summary>
        private static void GetDictionaryParaments() 
        {
            Paraments = new Dictionary<string, ParamentStruct>();

            foreach (DataRow dataRow in DataTableParaments.Rows)
            {
                string index = dataRow["Index"].ToString();
                string name = dataRow["Name"].ToString();
                string style = dataRow["CtrStyle"].ToString();
                string label = dataRow["CtrLabel"].ToString();
                string tip = dataRow["CtrTip"].ToString();
                string range = dataRow["CtrValueRange"].ToString();
                string value = dataRow["CtrValue"].ToString();
                string extname = dataRow["CtrExtName"].ToString();

                var paramentStruct = new ParamentStruct()
                {
                    Index = index,
                    Name = name,
                    CtrStyle = style,
                    CtrLabel = label,
                    CtrTip = tip,
                    CtrValueRange = range,
                    CtrValue = value,
                    CtrExtName = extname
                };

                Paraments.Add(name, paramentStruct);
            }
        }


        /// <summary>
        /// 保存修改后的dataset到xml文件中
        /// </summary>
        public static void SaveDataSetParaments() 
        {
            if (DatasetParametns != null)
            {
                DatasetParametns.Tables.Clear();
                DatasetParametns.Tables.Add(DataTableParaments);
                GetDictionaryParaments();
                XmlUtility.SaveXmlSetting(DatasetParametns, XmlPath);
                IsXmlExist();
            }
        }

        /// <summary>
        /// 根据参数id获取参数对应的值
        /// </summary>
        /// <param name="paraIds"></param>
        /// <returns></returns>
        public static string[] GetParamentsValue(string[] paraIds)
        {
            List<string> listParaments = new List<string>();
            if (paraIds.Length > 0)
            {
                foreach (string paraId in paraIds)
                {
                    if (Paraments.ContainsKey(paraId))
                    {
                        listParaments.Add(Paraments[paraId].CtrValue);
                    }
                }
            }
            return listParaments.ToArray();
        }
    }
}
